package com.gpay.erp.web.control;

import com.gpay.erp.core.system.bean.*;
import com.gpay.erp.services.common.GlobalStatus;
import com.gpay.erp.services.system.impl.UserService;
import com.gpay.erp.web.util.WebUtil;
import com.gpay.erp.web.vo.ResponseInfo;
import com.gpay.erp.web.vo.RoleManage;
import com.gpay.erp.web.vo.UserSessionInfor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.support.RequestContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("/")
public class UserController extends BaseController {
	private Log log = LogFactory.getLog(UserController.class);
	@Autowired
	private UserService userService;
	@Autowired
	private MessageSource msg;
	@Autowired
	private GlobalStatus globleStatus;

	@RequestMapping("testJson")
	public void testJson(HttpServletRequest request,
			HttpServletResponse response) {
		// User user = new User();
		// user.setUsername("admin");
		// user.setPassword("123456");
		// WebUtil.responseToJson(response, user);
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		responseInfo.setResult("00000");
		// responseInfo.setMessage(requestContext.getMessage("user.username.again"));
		// responseInfo.setMessage(requestContext.getMessage("purchase.app.success"));
		responseInfo.setMessage(msg.getMessage("user.username.again",
				new Object[] {}, Locale.US));
		Map map = globleStatus.getSexMap();
		Map map1 = globleStatus.getContractStatus();
		responseInfo.setObject(map);
		responseInfo.setObject(map1);
		// PrintWriter out = response.getWriter();
		// out.print(responseInfo.toString());
		WebUtil.responseToJson(response, responseInfo);

	}

	// 主页页面跳转
	@RequestMapping("index")
	public String index(HttpServletRequest request, HttpServletResponse response) {
		return "index";

	}

	// 页面加载 主页中间部分
	@RequestMapping("portal/index")
	public String mainIndex(HttpServletRequest request,
			HttpServletResponse response) {
		return "portal/index";

	}

	// 页面加载 主页上部
	@RequestMapping("layout/north")
	public String north(HttpServletRequest request, HttpServletResponse response) {
		return "layout/north";

	}

	// 页面加载 主页右边
	@RequestMapping("layout/west")
	public String west(HttpServletRequest request, HttpServletResponse response) {
		return "layout/west";

	}

	@RequestMapping("loginPage")
	public String loginPage(HttpServletRequest request,
			HttpServletResponse response) {
		return "login";

	}

	@RequestMapping(value = "login")
	public String login(
			@ModelAttribute("userSessionInfor") UserSessionInfor userSessionInfor,
			HttpServletRequest request, HttpServletResponse response) {

		RequestContext requestContext = new RequestContext(request);

		ResponseInfo responseInfo = new ResponseInfo();
		HttpSession session = request.getSession();
		if ((userSessionInfor == null)
				|| (userSessionInfor.getUsername() == "")) {
			responseInfo.setResult("1");
			responseInfo.setMessage(requestContext
					.getMessage("user.username.validate.null",
							new Object[] { requestContext
									.getMessage("user.username") }));
			session.setAttribute("responseInfo", responseInfo);
			return "login";
		} else if (userSessionInfor.getPasswd() == "") {
			responseInfo.setResult("1");
			responseInfo.setMessage(requestContext.getMessage(
					"user.passwd.validate.null",
					new Object[] { userSessionInfor.getPasswd() }));
			session.setAttribute("responseInfo", responseInfo);
			return "login";
		}
		User user = new User();
		if (!"".equals(userSessionInfor.getPasswd())) {
			user.setUserName(userSessionInfor.getUsername());
			user.setUserPassword(userSessionInfor.getPasswd());
		}
		User user1 = userService.login(user);
		log.info("username:" + user.getUserName());
		if (user1 != null) {
			responseInfo.setResult("0");
			responseInfo.setMessage(userSessionInfor.getUsername());
			session.setAttribute("responseInfo", responseInfo);
			return "success";
		} else {
			responseInfo.setResult("1");
			responseInfo.setMessage(requestContext
					.getMessage("username.or.password.mistake"));
			session.setAttribute("responseInfo", responseInfo);

			return "login";
		}
	}

	@RequestMapping("getJsonValue")
	public void getJsonValue(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		responseInfo.setResult("00000");
		responseInfo.setMessage(requestContext
				.getMessage("user.username.again"));
		// PrintWriter out = response.getWriter();
		// out.print(responseInfo.toString());
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * //页面跳转 部门管理
	 * 
	 * @RequestMapping("department") public String department() { return
	 * "admin/department"; }
	 */

	// 页面跳转 角色授权
	@RequestMapping("roleGrant")
	public String roleGrant() {
		return "admin/roleGrant";
	}

	// 增加角色 仅测试 roleManage 的使用
	@RequestMapping("addRole")
	public void addRole(RoleManage roleManage, HttpServletResponse response) {
		WebUtil.responseToJson(response, roleManage);
	}

	// 页面跳转 用户增加
	@RequestMapping("addUser")
	public String addUser() {
		return "admin/userAdd";
	}

	// 页面跳转 订单列表
	@RequestMapping("order")
	public String order() {
		return "purchaseManage/order";
	}

	// 页面跳转 订单列表
	@RequestMapping("orderAdd")
	public String orderAdd() {
		return "purchaseManage/orderAdd";
	}

	// 页面跳转 订单编辑
	@RequestMapping("orderEdit")
	public String orderEdit() {
		return "purchaseManage/orderEdit";
	}

}